package com.itcaius.weblog.admin.service.Impl;

import com.itcaius.weblog.admin.model.vo.file.UploadFileRspVO;
import com.itcaius.weblog.admin.service.AdminFileService;
import com.itcaius.weblog.admin.utils.MinioUtil;
import com.itcaius.weblog.common.enums.ResponseCodeEnum;
import com.itcaius.weblog.common.exception.BizException;
import com.itcaius.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Caius
 * @description Minio 服务
 * @since Created in 2025-04-26
 */
@Service
@Slf4j
public class AdminFileServiceImpl implements AdminFileService {

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @Override
    public Response uploadFile(MultipartFile file) {
        try{
            String url = minioUtil.uploadFile(file);

            return Response.success(UploadFileRspVO.builder().url(url).build());
        } catch (Exception e) {
            log.warn("==> 文件上传至 Minio 错误: ", e);

            throw new BizException(ResponseCodeEnum.FILE_UPLOAD_FAILED);
        }
    }
}
